package com.frxuexi.user.view

import android.text.TextUtils
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.User
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.viewmodel.UserNetViewModel
import kotlinx.coroutines.launch

@Composable
fun AddEmployeePage(navHostController: NavHostController,vm:UserNetViewModel= hiltViewModel()) {
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        Log.i("===",it.data.toString())
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })
    var uname by remember {
        mutableStateOf("")
    }
    var uno by remember {
        mutableStateOf("")
    }
    var phone by remember {
        mutableStateOf("")
    }
    var did by remember {
        mutableStateOf("")
    }
    var pid by remember {
        mutableStateOf("")
    }
    var pname by remember {
        mutableStateOf("")
    }
    Column(verticalArrangement = Arrangement.spacedBy(20.dp), modifier = Modifier.padding(20.dp)) {
        BaseTitle(title = "添加员工", navHostController = navHostController)
        Spacer(modifier = Modifier.height(10.dp))
        OutlinedTextField(value = uname, onValueChange ={uname=it}, Modifier.fillMaxWidth()
            , label = { Text(text = "用户名") }, leadingIcon = { Icon(
                imageVector = Icons.Default.AccountCircle,
                contentDescription = ""
            )
            }
        )
        OutlinedTextField(value = uno, onValueChange ={uno=it}, Modifier.fillMaxWidth()
            , label = { Text(text = "工号") }, leadingIcon = { Icon(
                imageVector = Icons.Default.AccountCircle,
                contentDescription = ""
            )
            }
        )
        OutlinedTextField(value = phone, onValueChange ={phone=it}, Modifier.fillMaxWidth()
            , label = { Text(text = "手机号") }, leadingIcon = { Icon(
                imageVector = Icons.Default.AccountCircle,
                contentDescription = ""
            )
            }
        )
        OutlinedTextField(value = did, onValueChange ={did=it}, Modifier.fillMaxWidth()
            , label = { Text(text = "部门编号") }, leadingIcon = { Icon(
                imageVector = Icons.Default.AccountCircle,
                contentDescription = ""
            )
            }
        )
        OutlinedTextField(value = pid, onValueChange ={pid=it}, Modifier.fillMaxWidth()
            , label = { Text(text = "职位编号") }, leadingIcon = { Icon(
                imageVector = Icons.Default.AccountCircle,
                contentDescription = ""
            )
            }
        )
        OutlinedTextField(value = pname, onValueChange ={pname=it}, Modifier.fillMaxWidth()
            , label = { Text(text = "职位名称") }, leadingIcon = { Icon(
                imageVector = Icons.Default.AccountCircle,
                contentDescription = ""
            )
            }
        )

        Button(modifier = Modifier.fillMaxWidth() ,onClick = {
            if(TextUtils.isEmpty(uname) || TextUtils.isEmpty(uno) || TextUtils.isEmpty(phone)){
                ToastUtils.showLong("输入不能为空")
                return@Button
            }
//            if(RegexUtils.isUsername())
            if(!uname.matches(Regex("[A-z0-9_]{3,10}"))){
                ToastUtils.showLong("用户名不合法")
                return@Button
            }
            val map = mutableMapOf<String, Any>()
            map["name"]=uname
            map["no"]=uno
            map["phone"]=phone
            map["did"]=did
            map["pid"]=pid
            map["pname"]=pname
            vm.sendIntent(UserIntent.addEmployee(map))
        }) {
            Text(text = "注册")
        }

    }

}